package com.zjx.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjx.reggie.common.R;
import com.zjx.reggie.dto.DishDto;
import com.zjx.reggie.entity.Category;
import com.zjx.reggie.entity.Dish;
import com.zjx.reggie.entity.DishFlavor;
import com.zjx.reggie.entity.SetmealDish;
import com.zjx.reggie.service.CategoryService;
import com.zjx.reggie.service.DishFlavorService;
import com.zjx.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {

    @Resource
    private DishService dishService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private DishFlavorService dishFlavorService;

    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String name) {
        Page<Dish> dishPage = new Page<>(page, pageSize);
        Page<DishDto> dishDtoPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotEmpty(name), Dish::getName, name);
        qw.orderByDesc(Dish::getUpdateTime);
        dishService.page(dishPage, qw);
        BeanUtils.copyProperties(dishPage, dishDtoPage);
        List<Dish> records = dishPage.getRecords();
        List<DishDto> list = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            String id = item.getCategoryId();
            Category category = categoryService.getById(id);
            dishDto.setCategoryName(category.getName());
            return dishDto;
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(list);
        return R.success(dishDtoPage);
    }

    @PostMapping()
    public R save(@RequestBody DishDto dishdto) {
        dishService.saveFlavor(dishdto);
        return R.success("菜品添加成功！");
    }

    @GetMapping("/{id}")
    public R getDish(@PathVariable String id) {
        return R.success(dishService.getByIdWithFlavor(id));
    }

    @PutMapping()
    public R update(@RequestBody DishDto dishDto) {
        dishService.updateFlavor(dishDto);
        return R.success("修改成功！");
    }

    @DeleteMapping()
    public R delete(@RequestParam("ids") List<String> ids){
        dishService.delete(ids);
        return R.success("删除成功！");
    }

    @PostMapping("/status/0")
    public R discontinued(@RequestParam List<String> ids){
        LambdaUpdateWrapper<Dish> qw = new LambdaUpdateWrapper<>();
        qw.in(Dish::getId,ids);
        qw.set(Dish::getStatus,0);
        dishService.update(qw);
        return R.success("商品已停售！");
    }


    @PostMapping("/status/1")
    public R sales(@RequestParam List<String> ids){
        LambdaUpdateWrapper<Dish> qw = new LambdaUpdateWrapper<>();
        qw.in(Dish::getId,ids);
        qw.set(Dish::getStatus,1);
        dishService.update(qw);
        return R.success("商品启售成功！");
    }
    @GetMapping("/list")
    public R list(Dish dish){
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());
        qw.eq(dish.getStatus() != null,Dish::getStatus,dish.getStatus());
        qw.like(dish.getName() != null,Dish::getName,dish.getName());
        List<Dish> list = dishService.list(qw);
        List<DishDto> dishDtos = list.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Category category = categoryService.getById(item.getCategoryId());
            dishDto.setCategoryName(category.getName());
            LambdaQueryWrapper<DishFlavor> dishQw = new LambdaQueryWrapper<>();
            dishQw.eq(DishFlavor::getDishId,item.getId());
            List<DishFlavor> dishFlavors = dishFlavorService.list(dishQw);
            dishDto.setFlavors(dishFlavors);
            return dishDto;
        }).collect(Collectors.toList());
        return R.success(dishDtos);
    }

}
